import sqlite3
from Queue import Queue
from threading import Thread

class db:
    def __init__(self, filename):
        self.__conn = sqlite3.connect(filename, check_same_thread = False)
        self.__csr = self.__conn.cursor()
        self.__csr.execute("create table if not exists data (lable text, content text)")
        self.__tasks = Queue()
        t = Thread(target=self.__worker)
        t.daemon = True
        t.start()
    def __insert(self, lable, content):
        self.__csr.execute("insert into data values (?,?)", (lable, content))
        self.__conn.commit()
    def __worker(self):
        while True:
            item = self.__tasks.get()
            self.__insert(*item)
            self.__tasks.task_done()
    def put(self, lable, content):
        self.__tasks.put((lable, content))
    def close(self):
        self.__tasks.join()
        self.__conn.close()

def main():
    b = db('test.sqlite')
    b.put('42', 'Yesterday You Said Tomorrow')
    b.close()
if __name__ == '__main__':
    main()
